<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 * 客户信息db管理
 */

require_once(dirname(__FILE__) . "/../common/EasyMysql.php");
require_once(dirname(__FILE__) . "/../config/ConfigDB.php");

class UserCom {

    function __construct() {
        $dbconfig = $GLOBALS["config"] ['database'];
        $mysql = new EasyMysql($dbconfig['hostname'], $dbconfig ['port'], $dbconfig ['username'], $dbconfig ['password'], $dbconfig ['db'], $dbconfig ['charset']);
        $this->mysql = $mysql;
    }

    function getPassword($account) {
        try {
            $sql = "select * from user where account='{$account}'";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret [0][0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }

    function getUserById($userid) {
        try {
            $sql = "select * from user where id='{$userid}'";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret [0][0];
            }
        } catch (Exception $ex) {
            return false;
        }
        return false;
    }

    function getUserlist($minlimit, $limitnum) {
        try {
            $sql = "select * from user limit {$minlimit},{$limitnum}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }

    function getAllNum() {
        try {
            $sql = "select count(1) from user";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret [0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }

    function addUser($account, $password, $type, $community = 0, $role = 0) {
        try {
            if ($type == 0) {
                $sql = "insert into user(account,`password`,role) values ('{$account}','{$password}',{$role})";
            } else {
                $sql = "insert into user(account,`password`,role,community_id) values ('{$account}','{$password}','{$role}','{$community}')";
            }
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }

    function deleteUser($id) {
        try {
            $sql = "delete from user where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }

    function updateUser($userid, $type, $password = 0, $community = "", $role = "") {

        try {
            if ($type != 0) {
                if ($password == 0) {
                    $sql = "update user set community_id='{$community}',role='{$role}' where id={$userid}";
                } else {
                    $sql = "update user set `password`='{$password}',community_id='{$community}',role='{$role}' where id={$userid}";
                }
            } else {
                $sql = "update user set `password`='{$password}' where id={$userid}";
            }
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }

}
